var ECharts;
$(document).ready(function () {
    require.config({
        baseUrl: ctx + '/resource/plugins/echarts4',
        paths: {
            echarts: 'echarts',
            roma: 'theme/roma',
        }
    });
    require(
        [
            'echarts',
            'roma'
        ]);
    //加载方法
    process();
});
var processList = [];//接口
processList.push("FM-10-32-001-01");//状态
// processList.push("PM-10-32-001-01");//延迟
function process() {
    var str = processList.toString();
    $.ajax({
            url: ctx + "/load/queryTable",
            type: "post",
            data: "EXT_UNIT_ID=" + EXT_UNIT_ID + "&RESU=" + str + "&DATE_TYPE=" + DATE_TYPE,
            success: function (data) {
                var map = JSON.parse(data);
                var list = map["FM-10-32-001-01"];
                // var list1 = map["PM-10-32-001-01"];
                var table = "";
                if (list.length == 0) {
                    table +=
                        "<tr><th><span >" + "指标名" + "</span></th></tr>" +
                        "<tr><td><span >" + "暂无数据" + "</span></td></tr>";

                } else {
                    table += "<tr>" +
                        "<th><span >" + "端口" + "</span></th>" +
                        "<th><span >" + "进程" + "</span></th>" +
                        "<th><span >" + list[0].KPI_NAME + "</span></th>" +
                            // "<th><span >" + list1[0].KPI_NAME + "</span></th>" +
                        "</tr>";
                    if (DATE_TYPE == "showYear") {
                        var UnitIdList1 = new Array()   //结果ID列表
                        var UnitIdList2 = new Array()
                        var AvgValueList = new Array() //结果VALUE列表
                        var succList = new Array();
                        var countList = new Array();
                        var map = new Map()            //结果map
                        var iSameLine = 0 //相同条数标志位

                        var SumValue = 0 //和值
                        var gsuccValue = 0
                        var gcountValue = 0
                        var sTreeUnitId   //上一次ID

                        var str           //原始unit_id
                        var sUnitId       //Unit_id
                        var UNITID1
                        var UNITID2
                        var succValue
                        var countValue
                        var AvgValue     //avg_value

                        // console.log('列表条数为' + list.length)
                        for (i = 0; i < list.length; i++) {
                            str = list[i].UNIT_ID;
                            sUnitId = str.split("-")[4];
                            UNITID1 = sUnitId.split(":")[0];
                            UNITID2 = sUnitId.split(":")[1];
                            if (i < list.length) {
                                succValue = parseInt(list[i].SUCCESS_VALUE);
                                countValue = parseInt(list[i].COUNT_VALUE);
                                // AvgValue = parseInt(list1[i].AVG_VALUE);
                            }
                            if (i == 0) {                    //第一次进入标志位强制相同
                                sTreeUnitId = sUnitId
                            }
                            if (sTreeUnitId == sUnitId) {   //本次循环获取id与上次相同累加并继续下次循环
                                gsuccValue = gsuccValue + succValue;
                                gcountValue = gcountValue + countValue;
                                SumValue = SumValue + AvgValue;
                                iSameLine = iSameLine + 1     //桩相同条数
                                if (i == list.length - 1) {   //循环到最后一次如果与上一次相同直接写出结果
                                    UnitIdList1.push(UNITID1)
                                    UnitIdList2.push(UNITID2)
                                    succList.push(gsuccValue);
                                    countList.push(gcountValue);
                                    AvgValueList.push(SumValue)
                                    /* map.set(sTreeUnitId,SumValue1)*/
                                    // console.log('存入完毕存入ID' + sTreeUnitId)
                                }
                            } else {
                                UnitIdList1.push(UNITID1)
                                UnitIdList2.push(UNITID2)
                                succList.push(gsuccValue);
                                countList.push(gcountValue);
                                AvgValueList.push(SumValue);
                                /*map.set(sTreeUnitId,SumValue1)*/
                                // console.log('存入完毕存入ID' + sTreeUnitId)
                                // console.log('循环到不相同位置上一次id为' + sTreeUnitId +  '  和为' + SumValue)
                                // console.log('------------------------华丽分割线---------------------------')
                                //循环到最后一次 如果与上一次不同写出结果 重置无所谓
                                //==重置标志位
                                iSameLine = 1
                                sTreeUnitId = sUnitId
                                gsuccValue = succValue;
                                gcountValue = countValue;
                                SumValue = AvgValue
                                // console.log('重置标志位 此时标志位id为' + sTreeUnitId + '  和为' + SumValue)
                            }

                        }
                        for (var c = 0; c < UnitIdList1.length; c++) {
                            if (c < UnitIdList1.length) {
                                table += "<tr>" +
                                    "<td><span >" + UnitIdList1[c] + "</span></td>" +
                                    "<td><span >" + UnitIdList2[c] + "</span></td>" +
                                    "<td><span >" + parseInt(succList[c] / countList[c] * 100 / iSameLine) + "%" + "</span></td>" +
                                        // "<td><span >" + AvgValueList[c] / iSameLine + "秒" + "</span></td>" +
                                    "</tr>";
                            }
                        }
                        $("#row7").css("height", (UnitIdList1.length + 1) * 34 + 14 + 34 + 34);
                    } else {
                        for (var i = 0; i < list.length; i++) {
                            var str = list[i].UNIT_ID;
                            var a = str.split("-")[4];
                            var UNITID1 = a.split(":")[0];
                            var UNITID2 = a.split(":")[1];
                            if (i < list.length) {
                                table += "<tr>" +
                                    "<td><span >" + UNITID1 + "</span></td>" +
                                    "<td><span >" + UNITID2 + "</span></td>" +
                                    "<td><span >" + parseInt((list[i].SUCCESS_VALUE / list[i].COUNT_VALUE) * 100) + "%" + "</span></td>" +
                                        // "<td><span >" + (list1[i].AVG_VALUE) + "秒" + "</span></td>" +
                                    "</tr>";
                            }
                        }
                        $("#row7").css("height", (list.length + 1) * 34 + 14 + 34 + 34);
                    }
                }
                $("#share").append(table);
            },
            error: function (textStatus) {
                console.log("error:" + textStatus.status);
            }
        }
    )
    ;

}

// $(".rad_1").click(function () {
//     var flag = $(this).attr('for');
//     $("#share tr").remove();
//
//     process(flag);
//
// });
